package com.ctz.commonframe.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.ctz.commonframe.R;
import com.ctz.commonframe.utils.Logger;

import org.xutils.x;

/**
 * Created by 陈堂正 on 2016/11/24.
 */
public class BaseFragment extends Fragment{
    private boolean isCreatedView;
    private boolean isVisible;
    protected Activity mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return x.view().inject(this, inflater, container);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isCreatedView = true;
        if(isVisible){
            onVisible();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isCreatedView = false;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = getUserVisibleHint();
        if (isCreatedView && isVisible){
            onVisible();
        } else {
            onInvisible();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        isVisible = getUserVisibleHint();
        if (isCreatedView && isVisible){
            onVisible();
        }
    }

    protected void onVisible() {

    }

    protected void onInvisible() {

    }

    @Override
    public void startActivity(Intent intent) {
        if(mContext instanceof BaseActivity){
            ((BaseActivity)mContext).startActivity(intent);
        }else{
            super.startActivity(intent);
        }
    }
}
